﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.UserClass
{
    public partial class edit : System.Web.UI.Page
    {
       
        RoleService roleService = new RoleService();
        static string CheckRoleName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id=Request["id"];
                string option = Request["OPERATION"];
                if (id == null)
                    return;
                if (option == "Modify")
                    Modify(id);
            }
        }
        protected void Modify(string id)
        {
            BoRoles ro = roleService.getRole(id);
            RoleId.Value = ro.ROL_ID;
            RoleName.Text = ro.ROL_NAME;
            CheckRoleName = ro.ROL_NAME;
            RoleType.SelectedValue = ro.ROL_TYPE == 0 ? "0" : "1";

        }
        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoRoles role = null;

            if (RoleId.Value == "")
                role = new BoRoles();
            else
                role = roleService.getRole(RoleId.Value);
            role.ROL_NAME = RoleName.Text;
            role.ROL_TYPE = Convert.ToInt32(RoleType.SelectedValue);
            if (role.ROL_ID == "")
            {
                if (roleService.CheckRoleName(RoleName.Text))
                {
                    Response.Write("<script>alert('该名字已经存在，不允许重复')</script>");
                    return;
                }
                else
                    roleService.CreateRole(role);
            }
            else
            {
                ///如果没有更改姓名
                if (CheckRoleName == RoleName.Text.Trim())
                {
                    roleService.UpdateRole(role);
                }
                ///如果更改了姓名，要检查更改后的姓名是否可用
                else
                {
                    if (roleService.CheckRoleName(RoleName.Text))
                    {
                        Response.Write("<script>alert('该名字已经存在，不允许重复')</script>");
                        return;
                    }
                    else
                        roleService.UpdateRole(role);
                }


            }
            
            
            Response.Redirect("List.aspx");
        }
        /// <summary>
        /// 重置事件，可以用js做
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_reset(object sender, EventArgs e)
        {
            RoleName.Text = "";
            RoleId.Value = "";
            RoleType.SelectedValue = "0";
        }
        /// <summary>
        /// 获得用户类别名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //protected string GetUserClass(int id) {
        //    string userclassTitle = new DbCommon.DbHelper().GetSingleItem("UserClass", "UserTitle", "UserId=" + id).ToString();
        //    return userclassTitle;
        //}
        ///// <summary>
        ///// 保存事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void btn_Save_Click(object sender, EventArgs e) {
        //    string classtitle = UserClassTitle.Value;
        //    if (!string.IsNullOrEmpty(classtitle))
        //    {
        //        int j = usercalss.updateTitle(Convert.ToInt32(Request.QueryString["id"]), classtitle);
        //        if (j > 0)
        //        {
        //            Response.Write("<script>alert('修改成功');</script>");
        //        }
        //        else
        //            Response.Write("<script>alert('修改失败')</script>");
        //    }
        //    else
        //        Response.Write("<script>alert('类别名称不允许为空')<script>");
        //}
    }
}